package com.zmn.plat.services.impl.push;

import java.util.*;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.zmn.common.dto.VtDTO;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.model.entity.push.notice.PushNoticeApp;
import com.zmn.plat.model.entity.push.notice.PushNoticeAppQuery;
import com.zmn.plat.model.entity.push.notice.PushNoticeAppSearchEngineerQuery;
import com.zmn.plat.persistence.interfaces.push.PushNoticeAppDao;
import com.zmn.plat.services.interfaces.push.notice.PushNoticeAppService;

import lombok.extern.slf4j.Slf4j;

/**
 * 描述:: App 公告推送基础接口实现类
 *
 * @author wangjie
 * @since 2020/3/21
 **/
@Slf4j
@Service
public class PushNoticeAppServiceImpl implements PushNoticeAppService {

    @Resource
    PushNoticeAppDao pushNoticeAppDao;

    /**
     * 描述: 根据主键查询数据
     *
     * @param key
     *            主键id
     * @return com.zmn.plat.model.entity.push.notice.PushNoticeApp
     * @author wangjie
     * @since 2020/3/21
     **/
    @Override
    public PushNoticeApp findByKey(Integer key) {

        if (NumberUtil.isNullOrZero(key)) {
            throw new NullPointerException("key is not null");
        }
        return pushNoticeAppDao.findByKey(key);
    }

    /**
     * 描述: 根据条件查询数据
     *
     * @param query
     *            查询对象
     * @return java.util.List<com.zmn.plat.model.entity.push.notice.PushNoticeApp>
     * @author wangjie
     * @since 2020/3/21
     **/
    @Override
    public Integer countPageByQuery(PushNoticeAppQuery query) {
        if (Objects.isNull(query)) {
            throw new NullPointerException("query is not null");
        }
        return pushNoticeAppDao.countPageByQuery(query);
    }

    /**
     * 描述: 根据条件查询数据
     *
     * @param query
     *            查询对象
     * @return java.util.List<com.zmn.plat.model.entity.push.notice.PushNoticeApp>
     * @author wangjie
     * @since 2020/3/21
     **/
    @Override
    public List<PushNoticeApp> listPageByQuery(PushNoticeAppQuery query) {

        if (Objects.isNull(query)) {
            throw new NullPointerException("query is not null");
        }
        return pushNoticeAppDao.listPageByQuery(query);
    }

    @Override
    public List<PushNoticeApp> listPushNoticeLessTimeAndStatus(Date time, Integer status) {

        time = Objects.isNull(time) ? DateUtil.getNow() : time;
        status = NumberUtil.isNullOrZero(status) ? GlobalConsts.YES : status;
        return pushNoticeAppDao.listPushNoticeLessTimeAndStatus(time, status);
    }

    /**
     * 描述: 查询推送工程师
     *
     * @param pushNoticeAppSearchEngineerQuery
     *            查询推送工程师
     * @return java.util.List<java.lang.Integer>
     * @author wangjie
     * @since 2020/3/31
     **/
    @Override
    public List<Integer> listPushNoticeToEngineer(PushNoticeAppSearchEngineerQuery pushNoticeAppSearchEngineerQuery) {
        if (Objects.isNull(pushNoticeAppSearchEngineerQuery)) {
            throw new NullPointerException("query is not null");
        }
        return pushNoticeAppDao.listPushNoticeToEngineer(pushNoticeAppSearchEngineerQuery);
    }

    /**
     * 描述: 查询有效的app公告数据
     *
     * @param putType
     *            推送类型
     * @param nowTime
     *            时间吗
     * @param status
     *            状态
     * @param putApp
     *            状态
     * @return java.util.List<com.zmn.plat.model.entity.push.notice.PushNoticeApp>
     * @author wangjie
     * @since 2020/4/1
     **/
    @Override
    public PushNoticeApp findPushByValidData(Integer putType, Date nowTime, Integer status, Integer putApp) {
        if (NumberUtil.isNullOrZero(putType) || Objects.isNull(nowTime) || NumberUtil.isNullOrZero(status)) {
            throw new NullPointerException("query is not null");
        }
        return pushNoticeAppDao.findPushByValidData(putType, nowTime, status, putApp);
    }

//    /**
//     * 描述: 获取全部工程师分组
//     * 兼容（待删除）
//     */
//    @Override
//    @Cacheable(cacheNames = "redis1d", key = "'notice:engineer:group:all'")
//    public List<VtDTO> listAllEngineerGroup() {
//
//        return Optional.ofNullable(pushNoticeAppDao.findAllGroup()).orElse(Collections.emptyList());
//    }

    /**
     * 描述: 根据key修改
     *
     * @param pushNoticeApp
     *            app公告对象 对象中必须有key值
     * @param operator
     *            操作人
     * @author wangjie
     * @since 2020/3/21
     **/
    @Override
    public void updateByKey(PushNoticeApp pushNoticeApp, String operator) {

        operator = Objects.isNull(operator) ? "" : operator;
        if (Objects.isNull(pushNoticeApp)) {
            throw new NullPointerException("pushNoticeApp is not null");
        }
        if (NumberUtil.isNullOrZero(pushNoticeApp.getId())) {
            throw new NullPointerException("key is not null");
        }
        pushNoticeApp.setUpdater(operator);
        pushNoticeApp.setUpdateTime(DateUtil.getNow());
        pushNoticeAppDao.updateByKey(pushNoticeApp);
    }

    /**
     * 描述: 修改排序号
     *
     * @param pushNoticeKey
     *            主键key
     * @param sort
     *            排序号
     * @param operator
     *            操作人
     * @author wangjie
     * @since 2020/3/21
     **/
    @Override
    public void updateSortByKey(Integer pushNoticeKey, Integer sort, String operator) {

        operator = Objects.isNull(operator) ? "" : operator;
        if (NumberUtil.isNullOrZero(pushNoticeKey) || Objects.isNull(sort)) {
            throw new NullPointerException("key or sort is not null");
        }
        PushNoticeApp pushNoticeApp = PushNoticeApp.builder().id(pushNoticeKey).sort(sort).build();
        this.updateByKey(pushNoticeApp, operator);
    }

    /**
     * 描述: 通过主键集合修改状态
     *
     * @param pushNoticeKeys
     *            主键key
     * @param status
     *            状态值
     * @param operator
     *            操作人
     * @author wangjie
     * @since 2020/3/21
     **/
    @Override
    public void updateStatusByKeys(List<Integer> pushNoticeKeys, Integer status, String operator) {

        operator = Objects.isNull(operator) ? "" : operator;
        if (CollectionUtil.isNullOrEmpty(pushNoticeKeys) || NumberUtil.isNullOrZero(status)) {
            throw new NullPointerException("keys or status is not null or empty");
        }
        pushNoticeAppDao.updateStatusByKeys(pushNoticeKeys, status, operator);
    }

    /**
     * 描述: 新增数据
     *
     * @param pushNoticeApp
     *            app公告对象
     * @param operator
     *            操作人
     * @return java.lang.Integer 新增后的id
     * @author wangjie
     * @since 2020/3/21
     **/
    @Override
    public Integer insert(PushNoticeApp pushNoticeApp, String operator) {

        operator = Objects.isNull(operator) ? "" : operator;
        if (Objects.isNull(pushNoticeApp)) {
            throw new NullPointerException("pushNoticeApp is not null");
        }
        pushNoticeApp.setCreater(operator);
        pushNoticeApp.setCreateTime(DateUtil.getNow());
        pushNoticeApp.setUpdater(operator);
        pushNoticeApp.setUpdateTime(DateUtil.getNow());
        return pushNoticeAppDao.insert(pushNoticeApp);
    }
}
